package hfu.pm.bll;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeMessage.RecipientType;

import hfu.pm.domain.DAO;
import hfu.pm.domain.DAOT;
import hfu.pm.domain.DAOT.Transactable;
import hfu.pm.entities.Account;
import com.google.appengine.api.users.User;
import com.googlecode.objectify.Key;

public class BusinessLogic {

	static DAO dao = new DAO();
	
	public static <T> T get(Key<T> key) {
		return dao.get(key);
	}
	
	public static <T> T get(String key) {
		return dao.get(key);
	}

	public static boolean hasAccount(User user) {
		Iterable<Account> account = dao.ofy().query(Account.class)
				.filter("googleUserId", user.getUserId());
		return account.iterator().hasNext();
	}

	public static Account getAccount(User user) {
		return dao.ofy().query(Account.class)
				.filter("googleUserId", user.getUserId()).get();
	}

	public static Account getAccount(String googleID) {
		return dao.ofy().query(Account.class).filter("googleUserId", googleID)
				.get();
	}

	public static Account getAccountByEmail(String email) {
		return dao.ofy().query(Account.class).filter("email", email).get();
	}

	public static void updateData(Account acc, User userData) {
		acc.setUser(userData);
		// acc.setGoogleUserId(userData.getUserId());
		acc.setTimestamp();
		dao.ofy().put(acc);
	}

	public static void registerAccount(User currentUser) {

		if (!hasAccount(currentUser)) {
			// This google account hasn't been in the system, add it
			final Account acc = new Account();
			// acc.setGoogleUserId(currentUser.getUserId());
			acc.setUser(currentUser);
			DAOT.runInTransaction(new Transactable() {
				
				@Override
				public void run(DAOT daot) {
					dao.ofy().put(acc);
					ContactLogic.addToContactList(acc, acc);				
				}
			});			
		}
	}

	// send invitation email to new user 
	public static int sendInvitationEmail(Account sender, String email, String url) {
		if (getAccountByEmail(email) == null) {
			Properties props = new Properties();
			Session session = Session.getDefaultInstance(props, null);
			String nick = sender.getUser().getNickname();
			String body = nick
					+ " wants you to try Poke-Message! "
					+ "<br/>Poke-message lets you bring all your photos, docs and videos with you anywhere and share them easily.<br/>";
			//String link = "<a href=\"" + url + "\">" + "Get started and become " + nick + "'s friend" + "</a>";
			String link = "Get started and become " + nick + "'s friend<br/>" + url;
			body += link;

			String subject = nick + " invited you to Poke-Message";
			
			try {
				MimeMessage msg = new MimeMessage(session);
				msg.setFrom(new InternetAddress(sender.getUser().getEmail(), sender
						.getUser().getNickname()));
				msg.addRecipient(RecipientType.TO, (new InternetAddress(email)));
				msg.setSubject(subject);
				
//				MimeBodyPart msgBodyPart = new MimeBodyPart();
//				msgBodyPart.setText(body, "UTF-8", "html");
//				
//				Multipart multipart = new MimeMultipart();
//				multipart.addBodyPart(msgBodyPart);
//				
//				msg.setContent(multipart);
				msg.setText(body, "UTF-8", "html");
				
				Transport.send(msg);
				return 0;
			} catch (AddressException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return -1;
		} 
		return 1;
	}
}
